﻿//-----------------------------------------------------------------------------------------------------------------
//  Version | DateTime(Create Or Update)  |  UserName		              |  Description						                  |
//-----------------------------------------------------------------------------------------------------------------
//  1.0		  | 2025/08/01 15:40:58		      | WEI.ZHOU (Willis)	        |  Create Program.cs Class
//-----------------------------------------------------------------------------------------------------------------

using Common.Framework.Launcher.Utils;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

// ReSharper disable UnusedParameter.Local
// ReSharper disable UnusedVariable
namespace Common.Framework.Launcher
{
  /// <summary>
  /// 程序的入口点。
  /// </summary>
  internal static class Program
  {
    /// <summary>
    /// INIHelper
    /// </summary>
    private static readonly INIHelper iNIHelper =
      new INIHelper(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Launcher.ini"));

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      //Application.Run(new FormMain());
      FormMain splashScreen = new FormMain();
      if (splashScreen.ShowDialog() == DialogResult.OK)
      {
        string sLauncherAppName = iNIHelper.Read("LauncherSetting", "LauncherAppName", "Common.Framework.Launcher");
        string sLauncherAppPath = iNIHelper.Read("LauncherSetting", "LauncherAppPath", "Common.Framework.Launcher");
        string sLauncherAppParameter =
          iNIHelper.Read("LauncherSetting", "LauncherAppParameter", "Common.Framework.Launcher");


        // 获取目标程序的完整路径
        string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, sLauncherAppPath);

        // 获取目标程序所在的目录
        string workingDirectory = Path.GetDirectoryName(fullPath);

        // 设置 ProcessStartInfo
        if (workingDirectory != null)
        {
          ProcessStartInfo startInfo = new ProcessStartInfo
          {
            FileName = fullPath, // 目标程序的完整路径
            Arguments = sLauncherAppParameter, // 启动参数
            WorkingDirectory = workingDirectory, // 设置工作目录为目标程序所在的目录
            Verb = "runas", // 以管理员身份运行
            UseShellExecute = true // 必须设置为 true 才能使用 Verb
          };

          // 启动进程
          try
          {
            Process process = Process.Start(startInfo);
          }
          catch (System.ComponentModel.Win32Exception ex)
          {
            // 如果用户取消了 UAC 提示，会抛出异常
            MessageBox.Show($@"Program startup failed {ex.Message}");
          }
        }
      }
      else
      {
        Application.Exit();
      }
    }
  }
}